//
//  UIImageView+extention.swift
//  Weibo
//
//  Created by 唐润欣 on 16/6/17.
//  Copyright © 2016年 唐润欣. All rights reserved.
//

import UIKit
import SDWebImage

extension UIImageView {
    
    //1. 便利构造器前面要使用convenience修饰
    convenience init(imageName: String) {
        
        //2. 便利构造器，总是调用自己的指定构造器完成构造过程
        self.init()
        
        let image = UIImage(named: imageName)
        self.image = image
    }
    
    ///  封装异步加载图片的方法
    ///
    ///  - parameter urlString:   url字符串
    ///  - parameter placeholder: 占位图像名
    func setImage(urlString: String?, placeholder: String) {
        
        guard let urlString = urlString, url = NSURL(string: urlString) else {
            
            return
        }
        
        self.sd_setImageWithURL(url, placeholderImage: UIImage(named: placeholder))
    }
    
    func setCircleImage(urlString: String?, placeholder: String, isCornered: Bool = false, size: CGSize, color: UIColor = UIColor.whiteColor()) {
        
        guard let urlString = urlString, url = NSURL(string: urlString) else {
            
            return
        }
        
        SDWebImageManager.sharedManager().downloadImageWithURL(url, options: [], progress: nil) { (downloadImage, _, _, _, _) -> Void in
            
            downloadImage.createCircleImage(isCornered, size: size, color: color)
            downloadImage.async_createCircleImage(isCornered, size: size, color: color, completion: { (circleImage) -> () in
                
                self.image = circleImage
            })
        }
    }
}
